﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NetCoreClass
{
    using System;
    using System.Collections.Generic;
    using System.Dynamic;
    using System.Reflection;

    public static class ObjectConverter
    {
        public static object ConvertLongToString<T>(T obj) where T : class
        {
            // 获取对象的类型
            Type type = obj.GetType();

            // 获取对象中的所有属性
            PropertyInfo[] properties = type.GetProperties();

            // 创建一个字典，用于存储新对象中的属性值
            Dictionary<string, object> propertyValues = new Dictionary<string, object>();

            dynamic newObj = new ExpandoObject();
            var newObjDict = (IDictionary<string, object>)newObj;
            // 遍历对象中的所有属性
            foreach (PropertyInfo property in properties)
            {
                // 获取属性名称和值
                string propertyName = property.Name;
                object propertyValue = property.GetValue(obj);

                // 如果属性类型为long，则将其转换为字符串类型
                if (property.PropertyType == typeof(long))
                {
                    propertyValue = propertyValue.ToString();
                }

                // 将属性名称和值添加到字典中
                propertyValues.Add(propertyName, propertyValue);
                // 将属性名称和值添加到新对象中
                newObjDict[propertyName] = propertyValue;
            }

            foreach (var item in propertyValues)
            {
                PropertyInfo prop = newObj.GetType().GetProperty(item.Key, BindingFlags.Public | BindingFlags.Instance);
                if (prop != null && prop.CanWrite)
                    prop.SetValue(newObj, item.Value, null);
            }

            return newObj;
        }
    }
}
